package com.atguigu.yygh.order.controller;


import com.atguigu.yygh.enums.OrderStatusEnum;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.order.service.OrderInfoService;
import com.atguigu.yygh.result.R;
import com.atguigu.yygh.utils.JwtHelper;
import com.atguigu.yygh.vo.order.OrderCountQueryVo;
import com.atguigu.yygh.vo.order.OrderQueryVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2025-03-26
 */
@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderInfoController {
    @Autowired
    private OrderInfoService orderInfoService;

    @ApiOperation("创建订单")
    @PostMapping("auth/submitOrder/{scheduleId}/{patientId}")
    public R submitOrder(@PathVariable("scheduleId") String scheduleId,
                         @PathVariable("patientId") Long patientId) {
        Long orderId = orderInfoService.submitOrder(scheduleId, patientId);
        return R.ok().data("orderId", orderId);
    }

    @GetMapping("/auth/getPageList/{page}/{limit}")
    public R getPageList(@PathVariable("page") Integer page,
                         @PathVariable("limit") Integer limit,
                         OrderQueryVo orderQueryVo,
                         @RequestHeader String token) {
        Long userId = JwtHelper.getUserId(token);
        orderQueryVo.setUserId(userId);
        Page<OrderInfo> pageList=orderInfoService.getPageList(page, limit, orderQueryVo);
        return R.ok().data("pageModel",pageList);
    }

    @GetMapping("/auth/getStatusList")
    public R getStatusList(){

        return R.ok().data("statusList", OrderStatusEnum.getStatusList());
    }

    //订单详情
    @ApiOperation("订单详情")
    @GetMapping("/auth/getOrders/{orderId}")
    public R orderDetail(@PathVariable("orderId") Long orderId) {
        OrderInfo orderInfo = orderInfoService.selectDetail(orderId);
        return R.ok().data("orderInfo", orderInfo);
    }

    //内部调用,根据日期分组统计预约数量
    @PostMapping("/auth/getStatisticalByDate")
    public R getStatisticalByDate(@RequestBody OrderCountQueryVo orderCountQueryVo) {
        Map<String,Object> resultMap =orderInfoService.getStatisticalByDate(orderCountQueryVo);
        return R.ok().data(resultMap);
    }
}

